<template>
  <div>
    <el-card>
      <!-- 1. 二级分区级联选择 -->
      <el-row :gutter="20" style="margin-bottom: 15px">
        <el-col :span="6">
          <el-cascader
            v-model="cascaderTid"
            :options="cascaderOptions"
            :props="{ expandTrigger: 'hover', emitPath: false, checkStrictly: true }"
            placeholder="全部分区"
            clearable
            style="width: 100%"
          />
        </el-col>
      </el-row>

      <!-- 2. 排行榜表格 -->
      <el-table :data="currentPageData" border stripe v-loading="loading">
        <el-table-column label="排名" width="70" type="index" :index="rankIndex" fixed />
        <el-table-column label="封面" width="100" fixed>
          <template #default="{ row }">
            <el-image
              style="width: 80px; height: 45px; border-radius: 4px; object-fit: cover"
              :src="row.pic"
              fit="cover"
              referrerpolicy="no-referrer"
            />
          </template>
        </el-table-column>
        <el-table-column label="BVID" width="150" fixed>
          <template #default="{ row }">
            <el-link :href="`https://www.bilibili.com/video/${row.bvid}`" target="_blank" type="primary">
              {{ row.bvid }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="用户ID" prop="mid" width="120" fixed>
          <template #default="{ row }">
            <el-link :href="`https://space.bilibili.com/${row.mid}`" target="_blank" type="success">
              {{ row.mid }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" show-overflow-tooltip fixed />
        <el-table-column label="播放量" width="110">
          <template #default="{ row }">{{ num(row.view) }}</template>
        </el-table-column>
        <el-table-column label="弹幕数" width="90">
          <template #default="{ row }">{{ num(row.danmaku) }}</template>
        </el-table-column>
        <el-table-column label="点赞数" width="90">
          <template #default="{ row }">{{ num(row.like) }}</template>
        </el-table-column>
        <el-table-column label="标签" width="200" show-overflow-tooltip>
          <template #default="{ row }">{{ row.tag.join('，') }}</template>
        </el-table-column>
      </el-table>

      <!-- 3. 分页 -->
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="query.current"
        :page-size="query.size"
        :total="total"
        :page-sizes="[10, 15, 25, 50, 100]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        style="margin-top: 15px"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed, watch, onMounted } from 'vue'
import { useApi } from '@/composables/useApi'
import partitionData from '@/assets/partition.json'

const http = useApi()

// 响应式数据
const loading = ref(false)
const total = ref(0)
const allData = ref([]) // 存储所有数据
const query = ref({ current: 1, size: 10 })
const cascaderTid = ref('') // 级联选中的末级 tid
const cascaderOptions = ref(partitionData) // 从JSON文件加载分区数据

// 计算属性 - 处理查询用的tid
const queryTid = computed(() => {
  return cascaderTid.value || ''
})

// 计算当前页需要展示的数据
const currentPageData = computed(() => {
  const start = (query.value.current - 1) * query.value.size
  const end = start + query.value.size
  return allData.value.slice(start, end)
})

// 监听分区变化，重置页码并重新获取数据
watch(queryTid, () => {
  query.value.current = 1
  getList()
})

// 页面加载时获取数据
onMounted(() => {
  getList()
})

// 方法定义
const getList = async () => {
  loading.value = true
  try {
    const params = queryTid.value ? { tid: queryTid.value } : {}

    const data = await http.get('/video/getHotVideo', { params })

    if (data.code === 200) {
      // 存储所有数据
      allData.value = data.data || []
      // 总条数等于所有数据的长度
      total.value = allData.value.length
    }
  } catch (error) {
    console.error('获取热门视频列表失败:', error)
    allData.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

const handleSizeChange = (val) => {
  // 改变每页条数时，重置到第一页
  query.value.size = val
  query.value.current = 1
  // 不需要重新请求，直接通过计算属性更新
}

const handleCurrentChange = (val) => {
  query.value.current = val
  // 不需要重新请求，直接通过计算属性更新
}

const rankIndex = (index) => {
  return (query.value.current - 1) * query.value.size + index + 1
}

const num = (val) => {
  return Number(val).toLocaleString()
}
</script>

<style scoped>
.el-image {
  display: block;
}
</style>
